#include <iostream>
#include "GraphOrientedMatrix.h"
#include <vector>

using namespace std;

GraphOrientedMatrix::GraphOrientedMatrix() { //cerr<<"Constructeur par défaut de GraphOrientedMatrix"<<endl;
}

GraphOrientedMatrix::GraphOrientedMatrix(const GraphOriented &g)
{
  //cerr<<"Constructeur par copie de GraphOrientedMatrix"<<endl;
  summit_nb = g.getSummitNb();
  edges_nb = g.getEdgesNb();
  orientation = new bool * [summit_nb];
  for (summit_t i(0); i<summit_nb; i++)
  {
    orientation[i] = new bool [summit_nb];
    for (summit_t j(0); j<summit_nb; j++)
    {
      orientation[i][j]=g.areConnected(i,j);
    }
  }
}

GraphOrientedMatrix::GraphOrientedMatrix(summit_t n, edges_t m) : GraphOriented(n,m)
{
  //cerr<<"Constructeur paramétré de GraphOrientedMatrix"<<endl;
  orientation = new bool * [n];
  for (int i(0); i<n; i++)
  {
    orientation[i] = new bool [n];
    for (int j(0); j<n; j++)
    {
      orientation[i][j]=false;
    }
  }
  summit_nb = n;
  edges_nb = m;
}

GraphOrientedMatrix::~GraphOrientedMatrix()
{
  for (size_t i=0; i<getSummitNb(); i++)
  {
    delete [] orientation[i];
  }
  delete [] orientation;
}

bool GraphOrientedMatrix::areConnected(summit_t i, summit_t j) const
{
  if ((i<getSummitNb()) && (j<getSummitNb()) && (i>=0) && (j>=0))
    return orientation[i][j];
  return false;
}

void GraphOrientedMatrix::connect(summit_t i, summit_t j)
{
    if ((i<getSummitNb()) && (j<getSummitNb()) && (i>=0) && (j>=0))
      orientation[i][j]=true;
}

void GraphOrientedMatrix::deconnect(summit_t i, summit_t j)
{
  if ((i<getSummitNb()) && (j<getSummitNb()) && (i>=0) && (j>=0))
      orientation[i][j]=false;
}

vector<summit_t> * GraphOrientedMatrix::isNeighbourOf(summit_t v) const
{
  vector<summit_t> * parents = new vector<summit_t>;
  for (int i=0; i<summit_nb; i++)
  {
    if (orientation[i][v])
      parents->push_back(i);
  }
  if (parents->size()==0)
  {
    delete parents;
    return NULL;
  }
  return parents;
}
